import { message } from 'antd';
import axios from 'axios';

const request = axios.create({
    baseURL: '/api',
    timeout: 10000,
});

request.interceptors.request.use(config => {
    // 在发送请求之前做些什么
    return config;
});

request.interceptors.response.use(response => {
    const res = (response.data || {}) as ResType;
    const { errno, msg, data } = res;
    if (errno !== 0) {
        message.error(msg);
        throw new Error(msg);
    }
    return data as any;
});

export default request;

export type ResType = {
    errno: number;
    data?: DataType;
    msg?: string;
};
export type DataType = {
    [key: string]: any;
};
